package com.zhaoyh.framework.taglib;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class HeaderTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;

	private String maxAge = null;

	private String pageTitle = null;

	public int doEndTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

			if (StringUtils.isNotBlank(maxAge)) {
				int nMaxAge = Integer.parseInt(maxAge);
				HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
				response.setHeader("Pragma", "Pragma");
				response.setHeader("Cache-Control", "max-age=" + nMaxAge);
				response.setDateHeader("Expires", System.currentTimeMillis() + nMaxAge * 1000);
			}
			if (StringUtils.isNotBlank(pageTitle)) {
				request.setAttribute("pageTitle", pageTitle);
			}

			request.setAttribute("ctx", request.getContextPath());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return super.doEndTag();
	}

	public String getMaxAge() {
		return maxAge;
	}

	public void setMaxAge(String maxAge) {
		this.maxAge = maxAge;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}
}
